import sys
import time
import mysql.connector
import threading
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication,QHBoxLayout,QLineEdit,QTextEdit,QDesktopWidget
from PyQt5.QtGui import QTextCursor
from PyQt5.QtCore import pyqtSignal, QObject
from changefunction import parse_next_url, getrate,get_itemids,getdriver
from crawler import downloaddata
import pymysql
from contextlib import contextmanager
import mysql.connector
from mysql.connector import pooling
print(mysql.connector.__version__)
import queue

a=234+23

print(f"a = {a}")

# 添加这行来保持程序运行
# input("按回车键退出...")
def getconn(pool):
    conn = pool.get_connection()
    return conn
def getconn2(pool):
    conn = pool.get_connection()
    return conn
class MessageSender(QObject):
    signal = pyqtSignal(str)


class Window(QWidget):

    def __init__(self):
        super().__init__()

        layout_h = QHBoxLayout()
        config = {
            "user": "root",
            "password":"Zb_200407",
            "host": "gz-cdb-d4j7h16x.sql.tencentcdb.com",
            "database": "eas",
            'port':22333,
            'connect_timeout': 10,
            'charset': 'utf8mb4',
            'pool_reset_session': True,
            'use_pure': True
        }
        self.resume_button = QPushButton("启动任务")
        self.resume_button.clicked.connect(self.resume_thread)
        self.pause_button = QPushButton("停止任务")
        self.pause_button.clicked.connect(self.pause_thread)
        self._pause_event = threading.Event()
        self._pause_event.clear()  #
        self.setGeometry(300, 300, 800, 500)
        self.setWindowTitle('跟价改价-RPA')
        text_layout = QHBoxLayout()
        self.textmemo = QTextEdit()
        text_layout.addWidget(self.textmemo)
        self.sender = MessageSender()
        self.sender.signal.connect(self.textmemo.append)
        layout_h.addWidget(self.resume_button)

        self.textmemo.append(
            '本系统负责跟价改价')
        self.textmemo.append(
            '请在第一个打开的谷歌浏览器登录店小秘ebay账号，在第二个浏览器登录aliexpress店小秘账号')
        # layout_h.addWidget(self.pause_button)

        main_layout = QVBoxLayout()
        main_layout.addLayout(layout_h)
        main_layout.addLayout(text_layout)
        self.setLayout(main_layout)
        self.thread = []
        shoptype = ['ebay', 'ali']
        # shoptype = ['ali']
        pool = mysql.connector.pooling.MySQLConnectionPool(
            pool_name='mypool',
            pool_size=10,
            **config
        )
        conn = getconn(pool)
        # for i,shoptype in enumerate(shoptype):
        #     y=get_itemids(conn,shoptype)
        #     print(y)
        # # self.drivers = getdriver()
        # desktop = QDesktopWidget()
        # width = desktop.screenGeometry().width()
        # height = desktop.screenGeometry().height()
        # self.drivers[0].set_window_position(0, int(height * 0.05))
        # self.drivers[0].set_window_size(width / 2, height)
        # # 设置第二个浏览器在右边
        # self.drivers[1].set_window_position(width / 2, int(height * 0.25))
        # self.drivers[1].set_window_size(width / 2, height)
        # tis = threading.Thread(target=downloaddata,args=(conn,))
        # self.thread.append(tis)

        for index, sptype in enumerate(shoptype):
            driver = getdriver()
            lockdianxiaomi = threading.Lock()
            conn = getconn(pool)
            tis = threading.Thread(target=parse_next_url,
                                   args=(conn,  sptype,driver,lockdianxiaomi))
            self.thread.append(tis)
        #     # tis.start()
    def pause_thread(self):
        # self._pause_event.clear()
        ixx = 2

    def resume_thread(self):
        for i in range(2):
            self.thread[i].start()
        self.resume_button.setEnabled(False)
        # self._pause_event.set()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())
